package com.ny.view;

import com.ny.dao.OrderDao;
import com.ny.dao.StudentDao;
import com.ny.dao.impl.ProductDaoImpl;
import com.ny.dao.impl.StudentDaoImpl;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.ManagerService;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.ManagerServiceImpl;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.util.List;
import java.util.Scanner;

import static com.ny.view.IndexView.orderService;

public class ManagerView {
    private static ManagerService managerService = new ManagerServiceImpl();
    private static StudentService studentService = new StudentServiceImpl();
    private static ProductService productService = new ProductServiceImpl();
    private static OrderService orderService = new OrderServiceImpl();

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        System.out.println("管理员登录");
        System.out.print("用户名 ");
        String name = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        if (managerService.login(name, password) != null) {
            boolean running = true;
            while (running) {
                displayMenu();
                int choice = Integer.parseInt(scanner.nextLine());

                switch (choice) {
                    case 1:
                        manageStudents();
                        break;
                    case 2:
                        manageProducts();
                        break;
                    case 3:
                        manageOrders();
                        break;
                    case 4:
                        running = false;
                        System.out.println("退出系统");
                        break;
                    default:
                        System.out.println("请重新输入！！！");
                }
            }
        } else {
            System.out.println("登录失败，用户名或密码错误");
        }
    }

    private static void displayMenu() {
        System.out.println("\n管理员界面");
        System.out.println("1. 用户管理");
        System.out.println("2. 商品管理");
        System.out.println("3. 订单管理");
        System.out.println("4. 退出系统");
        System.out.print("请选择: ");
    }

    private static void manageStudents() {
        // 学生管理菜单
        System.out.println("\n用户管理界面");
        System.out.println("1. 查看所有用户");
        System.out.println("2. 添加用户");
        System.out.println("3. 修改用户信息");
        System.out.println("4. 删除用户");
        System.out.print("请选择： ");
        int choice = Integer.parseInt(scanner.nextLine());

        try {
            switch (choice) {
                case 1:
                    viewAllStudents();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                default:
                    System.out.println("请重新输入！！！");
            }
        } catch (Exception e) {
            System.out.println("操作错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void viewAllStudents() throws Exception {
        List<Student> students = studentService.findAllStudents();
        System.out.println("\n所有用户列表界面");
        for (Student student : students) {
            System.out.println("ID: " + student.getStudentID() +
                    ", 姓名: " + student.getName() +
                    ", 手机: " + student.getPhone());
        }
    }

    private static void addStudent() {
        try {
            Student student = new Student();
            System.out.print("请输入姓名: ");
            student.setName(scanner.nextLine());
            System.out.print("请输入密码: ");
            student.setPassword(scanner.nextLine());
            System.out.print("请输入手机号: ");
            student.setPhone(scanner.nextLine());

            if (studentService.insertStudent(student) > 0) {
                System.out.println("用户添加成功");
            } else {
                System.out.println("用户添加失败");
            }
        } catch (Exception e) {
            System.out.println("添加用户时发生错误: " + e.getMessage());
        }
    }

    private static void updateStudent() {
        try {
            System.out.print("请输入要修改的用户ID: ");
            int id = Integer.parseInt(scanner.nextLine());

            Student student = studentService.getStudentById(id);
            if (student == null) {
                System.out.println("未找到该用户");
                return;
            }

            System.out.println("当前信息 姓名: " + student.getName() + ", 手机: " + student.getPhone());
            System.out.print("请输入新姓名 : ");
            String name = scanner.nextLine();
            if (!name.isEmpty()) {
                student.setName(name);
            }

            System.out.print("请输入新手机号: ");
            String phone = scanner.nextLine();
            if (!phone.isEmpty()) {
                student.setPhone(phone);
            }

            System.out.print("请输入新密码: ");
            String password = scanner.nextLine();
            if (!password.isEmpty()) {
                student.setPassword(password);
            }

            if (studentService.updateStudent(student) > 0) {
                System.out.println("用户信息更新成功");
            } else {
                System.out.println("用户信息更新失败");
            }
        } catch (Exception e) {
            System.out.println("更新用户信息时发生错误: " + e.getMessage());
        }
    }

    private static void deleteStudent() {
        try {
            System.out.print("请输入要删除的用户ID: ");
            int id = Integer.parseInt(scanner.nextLine());

            Student student = studentService.getStudentById(id);
            if (student == null) {
                System.out.println("未找到该用户");
                return;
            }

            System.out.print("确定要删除用户吗" + student.getName() + "? (y/n): ");
            String confirm = scanner.nextLine()
;
            if ("y".equalsIgnoreCase(confirm)) {
                if (studentService.deleteStudent(id) > 0) {
                    System.out.println("用户删除成功");
                } else {
                    System.out.println("用户删除失败");
                }
            }
        } catch (Exception e) {
            System.out.println("删除用户时发生错误: " + e.getMessage());
        }
    }

    private static void manageProducts() {

        System.out.println("\n-商品管理页面");
        System.out.println("1. 查看所有商品");
        System.out.println("2. 添加商品");
        System.out.println("3. 修改商品");
        System.out.println("4. 删除商品");
        System.out.print("请选择操作 (1-4): ");
        int choice = Integer.parseInt(scanner.nextLine());

        try {
            switch (choice) {
                case 1:
                    viewAllProducts();
                    break;
                case 2:
                    addProduct();
                    break;
                case 3:
                    updateProduct();
                    break;
                case 4:
                    deleteProduct();
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        } catch (Exception e) {
            System.out.println("操作发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void viewAllProducts() throws Exception {
        List<Product> products = productService.findAllProduct();
        System.out.println("\n商品列表界面");
        for (Product product : products) {
            System.out.println("ID: " + product.getProductId() +
                    ", 名称: " + product.getProductName() +
                    ", 库存: " + product.getStock() +
                    ", 价格: " + product.getPrice());
        }
    }

    private static void addProduct() {
        try {
            Product product = new Product();
            System.out.print("请输入商品名称: ");
            product.setProductName(scanner.nextLine());
            System.out.print("请输入商品库存数量: ");
            product.setStock(Integer.parseInt(scanner.nextLine()));
            System.out.print("请输入商品价格: ");
            product.setPrice(Double.parseDouble(scanner.nextLine()));
            System.out.print("请输入商品分类: ");
            product.setCategory(scanner.nextLine());
            System.out.print("请输入商品描述: ");
            product.setDescription(scanner.nextLine());

            if (productService.insertProduct(product) > 0) {
                System.out.println("商品添加成功");
            } else {
                System.out.println("商品添加失败");
            }
        } catch (Exception e) {
            System.out.println("添加商品时发生错误: " + e.getMessage());
        }
    }

    private static void updateProduct() {
        try {
            System.out.print("请输入要修改的商品ID: ");
            int id = Integer.parseInt(scanner.nextLine());

            Product product = productService.getProductById(id);
            if (product == null) {
                System.out.println("未找到该商品");
                return;
            }

            System.out.println("当前信息 名称: " + product.getProductName() +
                    ", 库存: " + product.getStock() +
                    ", 价格: " + product.getPrice());

            System.out.print("请输入新名称: ");
            String name = scanner.nextLine();
            if (!name.isEmpty()) {
                product.setProductName(name);
            }

            System.out.print("请输入新库存数量: ");
            String stockStr = scanner.nextLine();
            if (!stockStr.isEmpty()) {
                product.setStock(Integer.parseInt(stockStr));
            }

            System.out.print("请输入新价格: ");
            String priceStr = scanner.nextLine();
            if (!priceStr.isEmpty()) {
                product.setPrice(Double.parseDouble(priceStr));
            }

            System.out.print("请输入新分类: ");
            product.setCategory(scanner.nextLine());

            System.out.print("请输入新描述: ");
            product.setDescription(scanner.nextLine());

            if (productService.updateProduct(product) > 0) {
                System.out.println("商品信息更新成功");
            } else {
                System.out.println("商品信息更新失败");
            }
        } catch (Exception e) {
            System.out.println("更新商品信息时发生错误: " + e.getMessage());
        }
    }

    private static void deleteProduct() {
        try {
            System.out.print("请输入要删除的商品ID: ");
            int id = Integer.parseInt(scanner.nextLine());

            Product product = productService.getProductById(id);
            if (product == null) {
                System.out.println("未找到该商品");
                return;
            }

            System.out.print("确定要删除商品吗" + product.getProductName() + "? (y/n): ");
            String confirm = scanner.nextLine();

            if ("y".equalsIgnoreCase(confirm)) {
                if (productService.deleteProduct(id) > 0) {
                    System.out.println("商品删除成功");
                } else {
                    System.out.println("商品删除失败");
                }
            }
        } catch (Exception e) {
            System.out.println("删除商品时发生错误: " + e.getMessage());
        }
    }

    private static void manageOrders() {

        System.out.println("\n订单管理页面");
        System.out.println("1. 查看所有订单");
        System.out.println("2. 删除订单");
        System.out.print("请选择操作: ");
        int choice = Integer.parseInt(scanner.nextLine());

        try {
            switch (choice) {
                case 1:
                    viewAllOrders();
                    break;
                case 2:
                    deleteOrder();
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        } catch (Exception e) {
            System.out.println("操作发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void viewAllOrders() throws Exception {
        List<Order> orders = orderService.getAllOrders();
        System.out.println("\n订单列表");
        for (Order order : orders) {
            System.out.println("订单ID: " + order.getOrderId() +
                    ", 用户ID: " + order.getStudentId() +
                    ", 商品ID: " + order.getProductId() +
                    ", 数量: " + order.getNumber() +
                    ", 总价: " + order.getPrice());
        }
    }

    private static void deleteOrder() {
        try {
            System.out.print("请输入要删除的订单ID: ");
            int id = Integer.parseInt(scanner.nextLine());
            Order order = ManagerView.orderService.getOrderById(id);

            if (order == null) {
                System.out.println("未找到该订单");
                return;
            }

            System.out.print("确定要删除订单ID: " + id + "吗? (y/n): ");
            String confirm = scanner.nextLine();

            if ("y".equalsIgnoreCase(confirm)) {
                if (orderService.deleteOrder(id) > 0) {
                    System.out.println("订单删除成功");
                } else {
                    System.out.println("订单删除失败");
                }
            }
        } catch (Exception e) {
            System.out.println("删除订单时发生错误: " + e.getMessage());
        }
    }
}
